<?php

class Admin_Model_Student{

	private $student;
	
	function __construct(){
		$this->student = new Admin_Model_DbTable_Sinhviens();
		
	}
	
	public function listStudent($where=NULL){
		return $this->student->select()->where($where);
	}
	
	public function selectStudent($where=NULL, $order = 'u.ten',$limit =null){
		$dbApdater	= Zend_Db_Table::getDefaultAdapter();
		$dbSelect	= new Zend_Db_Select($dbApdater);
		
		$dbSelect->from(array('sv'	=> 'sinhvien'),'*')
				->joinLeft(array('u' => 'users'), 'u.username = sv.username',array('hoDem','ten','email','ngaySinh','quyen','password'))
				->joinLeft(
						array('l' => 'lop'),
					 	'l.id = sv.idLop',
						array('tenLop','khoa')
				)
				->joinLeft(
						array('d'=>'diem'),
					 	'd.username = sv.username',
						array('idMonHoc','diemHeSo1','diemHeSo2','diemThi','hocKy','namHoc')
				);
				
		if($where !=NULL){
			$dbSelect->where($where);	
		}
		
		$dbSelect->order($order);
		if ($limit != null) $dbSelect->limit($limit);
				
		return $dbSelect;
	}
	public function  addStudent($username,$idLop){
		$data =array(
			'username'	=>	$username,
			'idLop'		=>	$idLop
		);
		
		$this->student->insert($data);
	}
	
	public function updateStudent($username,$idLop){
		$this->student->update(array('idLop'=> $idLop), "username ='".$username."'");
	}
	
	public function deleteStudent($username){
		$this->student->delete("username = '".$username."'");
	}
	
	public function fetchRowStudent($username){
		return $this->student->fetchRow("username = '".$username."'");
	}
	
	public function fetchAllStudent($where=NULL){
		return $this->student->fetchAll($where);
	}
	
	public function fetchSql($sql){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		return $dbSelect = $dbAdapter->fetchAll($sql);
	}

}

